<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <img src="/vite.svg" alt="logo" class="logo" />
        <h2>欢迎登录</h2>
      </div>
      
      <Form
        ref="formRef"
        :model="formState"
        :rules="rules"
        class="login-form"
        @keyup.enter="handleSubmit"
      >
        <FormItem name="username">
          <Input
            v-model:value="formState.username"
            placeholder="用户名"
          >
            <template #prefix>
              <UserOutlined />
            </template>
          </Input>
        </FormItem>
        
        <FormItem name="password">
          <InputPassword
            v-model:value="formState.password"
            placeholder="密码"
          >
            <template #prefix>
              <LockOutlined />
            </template>
          </InputPassword>
        </FormItem>
        
        <FormItem>
          <Button
            type="primary"
            :loading="loading"
            class="w-full h-10"
            @click="handleSubmit"
          >
            登录
          </Button>
        </FormItem>
      </Form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/store/user'
import { Form, FormItem, Input, InputPassword, Button } from 'ant-design-vue'
import { UserOutlined, LockOutlined } from '@ant-design/icons-vue'
import type { FormInstance } from 'ant-design-vue'

const router = useRouter()
const userStore = useUserStore()
const formRef = ref<FormInstance>()

const formState = ref({
  username: '',
  password: ''
})

const rules = {
  username: [{ required: true, message: '请输入用户名' }],
  password: [{ required: true, message: '请输入密码' }]
}

const loading = ref(false)

const handleSubmit = async () => {
  try {
    loading.value = true
    await formRef.value?.validate()
    await userStore.loginAction(formState.value.username, formState.value.password)
    router.push('/')
  } catch (error) {
    console.error('登录失败:', error)
  } finally {
    loading.value = false
  }
}
</script>

<style scoped lang="less">
.login-container {
  height: 100vh;
  background-image: url('https://images.unsplash.com/photo-1451187580459-43490279c0fa?q=80&w=2072&auto=format&fit=crop');
  background-size: cover;
  background-position: center;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;

  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.2);
  }
  
  .login-box {
    position: relative;
    width: 400px;
    padding: 2.5rem;
    background: rgba(255, 255, 255, 0.9);
    border-radius: 16px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
    border: 1px solid rgba(255, 255, 255, 0.4);
    z-index: 1;
    animation: fadeIn 1s ease-out;
    
    .login-header {
      text-align: center;
      margin-bottom: 2rem;
      
      .logo {
        height: 64px;
        margin: 0 auto 1rem;
        filter: drop-shadow(0 0 8px rgba(0, 0, 0, 0.2));
        animation: float 6s ease-in-out infinite;
      }
      
      h2 {
        font-size: 1.75rem;
        font-weight: 600;
        color: #1a1a1a;
        margin: 0;
        text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
      }
    }
    
    .login-form {
      :deep(.ant-input-affix-wrapper) {
        height: 44px;
        border-radius: 8px;
        border: 1px solid rgba(0, 0, 0, 0.1);
        transition: all 0.3s;
        background: rgba(255, 255, 255, 0.9);
        
        &:hover, &:focus {
          border-color: #1890ff;
          box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.1);
          transform: translateY(-1px);
        }
        
        .ant-input {
          background: transparent;
        }
      }
      
      :deep(.ant-form-item) {
        margin-bottom: 1.5rem;
      }
      
      :deep(.ant-btn) {
        height: 44px;
        border-radius: 8px;
        font-size: 1rem;
        font-weight: 500;
        box-shadow: 0 2px 4px rgba(24, 144, 255, 0.2);
        background: linear-gradient(135deg, #1890ff 0%, #096dd9 100%);
        border: none;
        
        &:hover {
          transform: translateY(-1px);
          box-shadow: 0 4px 12px rgba(24, 144, 255, 0.4);
          background: linear-gradient(135deg, #40a9ff 0%, #1890ff 100%);
        }
      }
    }
  }
}

@keyframes pulse {
  0%, 100% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.1);
  }
}

@keyframes float {
  0%, 100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-10px);
  }
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style> 